﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using AcademicPlanner.Data;
using AcademicPlanner.Data.Entities;
using AcademicPlanner.Data.Repositories;
using AcademicPlanner.Framework.Libraries.Validation;
using AcademicPlanner.Framework.Helpers;

namespace AcademicPlanner.Domain.Models
{
	/// <summary>
	/// CourseCategory represents a second level hierarchy in the organization
	/// and grouping of academic courses.
	/// </summary>
	/// <since>1.0</since>
    public class CourseCategory : Model
    {
		/// <summary>
		/// The <c>CourseDivision</c> this CourseCategory belongs to.
		/// </summary>
		/// <since>1.0</since>
		protected CourseDivision division;

		/// <summary>
		/// Maxinum number of characters allowed for the <c>Code</c> property of a CourseCategory: <c>5</c>.
		/// </summary>
		/// <since>1.0</since>
		public static readonly int CODE_MAX_LENGTH = 5;


		/// <summary>
		/// Gets the data layer repository for this CourseCategory's underlying data representation.
		/// </summary>
		/// <since>1.0</since>
		protected override EntityRepository EntityRepo { get { return DomainMaster.DSAS.CourseCategoryEntityRepo; } }

		/// <summary>
		/// Gets this CourseCategory's underlying data representation.
		/// </summary>
		/// <since>1.0</since>
		protected CourseCategoryEntity Entity { get { return (CourseCategoryEntity)entity; } }

		/// <summary>
		/// Gets this CourseCategory's name.
		/// </summary>
		/// <since>1.0</since>
		public string Name
		{
			get
			{
				return Entity.Name;
			}
		}

		/// <summary>
		/// Gets this CourseCategory's code.
		/// </summary>
		/// <since>1.0</since>
		public string Code
		{
			get
			{
				return Entity.Code;
			}
		}

		/// <summary>
		/// Gets the <c>CourseDivision</c> this CourseCategory belongs to.
		/// </summary>
		/// <since>1.0</since>
		public CourseDivision Division
		{
			get
			{
				SetDivision();
				return division;
			}
		}


		/// <summary>
		/// Constructs a new CourseCategory.
		/// </summary>
		/// <remarks>
		/// This constructor invokes the matching super constructor.
		/// </remarks>
		/// <since>1.0</since>
		public CourseCategory()
			: base()
		{
			
		}

		/// <summary>
		/// Constructs a new CourseCategory with the provided <c>CourseCategoryEntity</c>.
		/// </summary>
		/// <remarks>
		/// This constructor invokes the matching super constructor.
		/// </remarks>
		/// <param name="pEntity">The CourseCategory's <c>CourseCategoryEntity</c>.</param>
		/// <since>1.0</since>
		public CourseCategory(CourseCategoryEntity pEntity) 
			: base(pEntity)
		{
			
		}


		private void SetDivision()
		{
			if (division == null)
				ResetDivision();
		}

		private void ResetDivision()
		{
			if (Entity.DivisionEntity != null)
				division = DomainMaster.ModelsFountain.GetCourseDivision(Entity.DivisionEntity);
		}

		internal void Set(string pName, string pCode)
		{
			var vErrs = Validate(pName, pCode);

			if (vErrs.OuterErrors.Count > 0)
				throw new InvalidModelPropertiesException(DomainErrorCode.M_INVALID_COURSECATEGORY_PROPERTIES, vErrs);
			
			SetDivision();
			if (division != null)
			{
				vErrs = division.ValidateCategory(entity.Id, pName ?? "", pCode ?? "");
				if (vErrs.OuterErrors.Count > 0)
					throw new InvalidModelPropertiesException(DomainErrorCode.M_INVALID_COURSECATEGORY_PROPERTIES, vErrs);
			}
			
			if (pName != null)
				Entity.Name = SecurityHelper.SanitizeInput(pName);
			if (pCode != null)
				Entity.Code = SecurityHelper.SanitizeInput(pCode).ToUpper();
		}

		/// <summary>
		/// Validates possible values of a CourseCategory's properties. <c>null</c> values are ignored.
		/// </summary>
		/// <remarks>
		/// <c>String</c> inputs are sanitized first.
		/// </remarks>
		/// <param name="pName">The name.</param>
		/// <param name="pCode">The code.</param>
		/// <returns>A Collection of validation errors.</returns>
		/// <seealso cref="M:ValidateName"/>
		/// <seealso cref="M:ValidateCode"/>
		/// <seealso cref="M:AcademicPlanner.Framework.Helpers.SecurityHelper.SanitizeInput"/>
		/// <since>1.0</since>
		public static InvalidModelPropertiesErrorCollection Validate(string pName, string pCode)
		{
			var rVal = new InvalidModelPropertiesErrorCollection("CourseCategory");
			InvalidModelPropertyErrorCode vCode;

			if (pName != null)
			{
				if (!(vCode = ValidateName(SecurityHelper.SanitizeInput(pName)))
						.Equals(InvalidModelPropertyErrorCode.NONE))
					rVal.AddOuter("Name", vCode);
			}

			if (pCode != null)
			{
				if (!(vCode = ValidateCode(SecurityHelper.SanitizeInput(pCode)))
						.Equals(InvalidModelPropertyErrorCode.NONE))
					rVal.AddOuter("Code", vCode);
			}

			return rVal;
		}

		/// <summary>
		/// Validates a possible value of a CourseCategory's name.
		/// </summary>
		/// <remarks>
		/// The value must not be blank.
		/// </remarks>
		/// <param name="pVal">The value to validate.</param>
		/// <returns>A validation error code.</returns>
		/// <seealso cref="M:AcademicPlanner.Framework.Libraries.Validation.StringValidator.IsNotBlank"/>
		/// <since>1.0</since>
		public static InvalidModelPropertyErrorCode ValidateName(string pVal)
		{
			var rVal = InvalidModelPropertyErrorCode.NONE;

			if (!StringValidator.IsNotBlank(pVal))
				rVal = InvalidModelPropertyErrorCode.REQUIRED;

			return rVal;
		}

		/// <summary>
		/// Validates a possible value of a CourseCategory's code.
		/// </summary>
		///  <remarks>
		/// The value must not be blank, contain at most <see cref="P:CODE_MAX_LENGTH"/>
		/// and be alpha.
		/// </remarks>
		/// <param name="pVal">The value to validate.</param>
		/// <returns>A validation error code.</returns>
		/// <seealso cref="M:AcademicPlanner.Framework.Libraries.Validation.StringValidator.IsNotBlank"/>
		/// <seealso cref="M:AcademicPlanner.Framework.Libraries.Validation.StringValidator.ContainsAtMost"/>
		/// <seealso cref="M:AcademicPlanner.Framework.Libraries.Validation.StringValidator.IsAlpha"/>
		/// <since>1.0</since>
		public static InvalidModelPropertyErrorCode ValidateCode(string pVal)
		{
			var rVal = InvalidModelPropertyErrorCode.NONE;

			if (!pVal.Equals(string.Empty))
			{
				if (!StringValidator.ContainsAtMost(pVal, CODE_MAX_LENGTH))
					rVal = InvalidModelPropertyErrorCode.TOO_LONG;
				else if (!StringValidator.IsAlpha(pVal))
					rVal = InvalidModelPropertyErrorCode.NOT_ALPHA;
			}

			return rVal;
		}
    }
}
